package kibahed.university.effizientealgorithmen.fileinteractions;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public class TspLibComparer {

	public static void main(String[] args) {
		String result = getResult("eil51");
		System.out.println(result);
	}

	public static String getResult(String tspFile) {
		if (tspFile != null) {
			if(tspFile.contains(".tsp")) {
				String help = tspFile.replace(".tsp", "");
				tspFile = help;
			}
			System.out.println("TSP name = " + tspFile);
			URL internetPfad = null;
			File res = null;
			try {
				internetPfad = new URL(
						"http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/STSP.html");
				// internetPfad = new URL("test.html");
				// res = new File("/test");
			} catch (Exception e) {
				e.toString();
			}

			if (internetPfad != null) {
				InputStreamReader reader = null;
				BufferedReader bf = null;

				try {

					reader = new InputStreamReader(internetPfad.openStream());
					bf = new BufferedReader(reader);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.toString();
				}
				if (bf != null) {
					try {
						for (String s; (s = bf.readLine()) != null;) {
							if (s.contains(tspFile)) {

								int startPos = s.lastIndexOf("<big>") + 5;
								int endPos = s.lastIndexOf("</big>");
								String result = s.substring(startPos, endPos);
								result = result.replaceAll(":", "");
								result = result.replaceAll(" ", "");
								result = result.replaceAll(tspFile, "");
								return result;

							}
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}
		}
		return "";
	}

}
